/**
 * Copyright 2014  XCL-Charts
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @Project XCL-Charts
 * @Description Android图表基类库
 * @author XiongChuanLiang<br></br>(xcl_168@aliyun.com)
 * @license http://www.apache.org/licenses/  Apache v2 License
 * @version 1.0
 */
package com.gitee.wsl.common.chart.listener.click

import android.graphics.RectF
import java.lang.Float

/**
 * @ClassName RectPosition
 * @Description  rect类型的位置记录信息基类
 * @author XiongChuanLiang<br></br>(xcl_168@aliyun.com)
 */
open class RectPosition : PositionRecord() {
    var rectF: RectF? = null
        protected set
   var rectFRange: RectF? = null

    //放大值
    var mExtValue = 0
    fun extPointClickRange(value: Int) {
        mExtValue = value
    }

    protected fun saveRectF(
        left: kotlin.Float,
        top: kotlin.Float,
        right: kotlin.Float,
        bottom: kotlin.Float
    ) {
        if (null == rectF) rectF = RectF()
        rectF!![left, top, right] = bottom
    }

    protected fun saveRectF(r: RectF?) {
        rectF = r
    }

    //MathHelper.getInstance().div(radius, 2.f);		
    val radius: kotlin.Float
        get() {
            var radius = 0.0f
            radius = rectF!!.bottom - mExtValue - (rectF!!.top + mExtValue)
            return radius //MathHelper.getInstance().div(radius, 2.f);		
        }
    val rectInfo: String
        get() {
            if (null == rectF) return ""
            val left = rectF!!.left + mExtValue
            val top = rectF!!.top + mExtValue
            val right = rectF!!.right - mExtValue
            val bottom = rectF!!.bottom - mExtValue
            return " left:" + Float.toString(left) + " top:" + Float.toString(top) +
                    " right:" + Float.toString(right) + " bottom:" + Float.toString(bottom)
        }

    override fun compareRange(x: kotlin.Float, y: kotlin.Float): Boolean {
        // TODO Auto-generated method stub
        if (null == rectF) return false
        if (null == rectFRange) rectFRange = RectF()
        rectFRange!!.setEmpty()
        rectFRange!!.set(rectF!!)
        rectFRange!!.left -= mExtValue.toFloat()
        rectFRange!!.top -= mExtValue.toFloat()
        rectFRange!!.right += mExtValue.toFloat()
        rectFRange!!.bottom += mExtValue.toFloat()

        //contains 在范围比较小时很不好使.
        if (rectFRange!!.contains(x, y)) return true

        //再加层手工检查
        return ((x.compareTo(rectFRange!!.left) == 1 || x.compareTo(rectFRange!!.left) == 0)
                && (x.compareTo(rectFRange!!.right) == -1 || x.compareTo(rectFRange!!.right) == 0)
                && (y.compareTo(rectFRange!!.bottom) == 1 || y.compareTo(rectFRange!!.bottom) == 0)
                && (y.compareTo(rectFRange!!.top) == -1 || y.compareTo(rectFRange!!.top) == 0))
    }
}